package com.itboye.lottery.fragment.gendan;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.itboye.lottery.base.BaseFragment;
import com.itboye.lottery.R;
import com.itboye.lottery.activity.follow.FollowDetailActivity;
import com.itboye.lottery.activity.follow.HotPeopleInfoActivity;
import com.itboye.lottery.adapter.GenDanCaseJinEAdapter;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.bean.FollowIndexBean;
import com.itboye.lottery.dialog.FollowDialog;
import com.itboye.lottery.fragment.GenDanFragment;
import com.itboye.lottery.presenter.UserPresenter;
import com.itboye.lottery.retrofit.ResponseCallback;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

/**
 * Created by Administrator (IntelliJ IDEA) on 2018/7/4.
 *
 * @description: Lottery
 */
public class GendanCaseJine extends BaseFragment implements ResponseCallback {

    RecyclerView rv_fangan_jine;
    private GenDanCaseJinEAdapter adapter;
    UserPresenter userPresenter = new UserPresenter(this);
    private ArrayList<FollowIndexBean.ListEntity> announcementBeanArrayList = new ArrayList<>();
    private boolean isInit=false;

    public static GendanCaseJine newInstance(int sort) {
        GendanCaseJine fragment = new GendanCaseJine();
        Bundle bundle = new Bundle();
        bundle.putInt("sort", sort);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_gendan_fanganjine;
    }

    @Override
    protected void initData() {
        sort = getArguments().getInt("sort");
        adapter = new GenDanCaseJinEAdapter(this, announcementBeanArrayList);
        rv_fangan_jine.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_fangan_jine.setNestedScrollingEnabled(false);
        rv_fangan_jine.setHasFixedSize(true);
        rv_fangan_jine.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        adapter.setOnItemClickListener((view, position) -> {
            startActivity(new Intent(getActivity(), FollowDetailActivity.class)
                            .putExtra("orderType", 0)
                            .putExtra("order_id", announcementBeanArrayList.get(position).getId())
                            .putExtra("type", announcementBeanArrayList.get(position).getLottery_type())//0:数字彩 1：足球  2：篮球
 //                        .putExtra("type",3001)
            );
        });
        rv_fangan_jine.setAdapter(adapter);
    }

    public int sort = 1, page = 1, page_size = 10, is_follow = 0;

    public void getFollowIndexData() {
        System.out.println("开始加载跟单");
        showDialog();
        userPresenter.getFollowIndex(sort, page, page_size,is_follow);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser&&!isInit) {
            isInit=true;
            getFollowIndexData();
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_follow:
                int position = (int) view.getTag();
                int multiple = (int) view.getTag(R.id.tag_1);
                FollowIndexBean.ListEntity listEntity = announcementBeanArrayList.get(position);
                FollowDialog followDialog = new FollowDialog(getActivity(), true);
                followDialog.setEntity(listEntity.getId(), listEntity.getLow_amount(), listEntity.getSingle_amount());
                followDialog.setMultiple(multiple);
                followDialog.show();
                break;
            case R.id.iv_user_icon:
                startActivity(new Intent(getActivity(), HotPeopleInfoActivity.class).putExtra("userId", view.getTag(R.id.tag_1).toString()));
                break;
        }
    }

    @Override
    public void success(@NotNull Object o, @NotNull String requestSuccessType) {
        dissDialog();
        if (requestSuccessType == userPresenter.getFollowIndex_SUCCESS) {
            FollowIndexBean followIndexBean = (FollowIndexBean) o;
            if (page == 1) {
                announcementBeanArrayList.clear();
            }
            announcementBeanArrayList.addAll(followIndexBean.getList());
            adapter.notifyDataSetChanged();
            try {
                ((GenDanFragment) getActivity().getSupportFragmentManager().getFragments().get(2)).pull_to_refresh_scrollView.onRefreshComplete();
            }catch (Exception e){

            }
        }
    }

    @Override
    public void fail(int code, @NotNull Object msg, @NotNull String requestErrorType) {
        dissDialog();
        if (requestErrorType == userPresenter.getFollowIndex_FAIL) {
            ByAlert.alert(msg);
            try {
                ((GenDanFragment) getActivity().getSupportFragmentManager().getFragments().get(2)).pull_to_refresh_scrollView.onRefreshComplete();
            }catch (Exception e){

            }
        }
    }
}
